//Created by Amit Mudgal (MicroStrategy Principal Consultant) for American Express GMS group on 4/29/2014

package com.AmericanExpress.LookandFeel;

import com.microstrategy.web.app.WebAppRuntimeException;
import com.microstrategy.web.app.WebAppSessionManager;
import com.microstrategy.web.app.beans.AppContext;
import com.microstrategy.web.beans.AggregatedWebFeatures;
import com.microstrategy.web.objects.WebFolder;
import com.microstrategy.web.objects.WebIServerSession;
import com.microstrategy.web.objects.WebObjectSource;
import com.microstrategy.web.objects.WebObjectsException;
import com.microstrategy.web.objects.WebObjectsFactory;
import com.microstrategy.web.objects.WebSearch;
import com.microstrategy.web.objects.admin.users.WebUser;
import com.microstrategy.web.objects.admin.users.WebUserGroup;
import com.microstrategy.webapi.EnumDSSXMLObjectSubTypes;
import com.microstrategy.webapi.EnumDSSXMLSearchDomain;
import com.microstrategy.webapi.EnumDSSXMLSearchFlags;

public class AdminFeatures extends AggregatedWebFeatures implements EnumCustomWebFeatures {
	
	private static String MAP_GROUP = "System Administrators";
	
	
	
	public boolean resolveCustomFeature(String featureId) {
        boolean result = true;
        if (MAP_GROUP_MEMBER_FEATURE.equals(featureId)) {
            result = resolveMapGroup();
        }
        return result;
    }
	
    private boolean resolveMapGroup() {
        boolean result = false;

        try {
            if (getWebComponent().getBeanContext() instanceof AppContext) {
                // Retrieve current user from Session manager
                WebAppSessionManager sm = ((AppContext) getWebComponent().getBeanContext()).getAppSessionManager();
                WebIServerSession session = sm.getActiveSession();
                // Retrieve user information from session
                WebUser user = ((WebUser) session.getUserInfo());
                user.populate();
                // Does the user belong to the MapGroup group?
                result = user.getParents().contains(getMapGroup());
            }
        } catch (WebObjectsException ex) {
            throw new WebAppRuntimeException("Unable to retrive UserInfo from the session.");
        }
        return result;
    }

    private WebUserGroup getMapGroup() {

        WebUserGroup mapGroup = null;

        try {
            if (getWebComponent().getBeanContext() instanceof AppContext) {

                WebAppSessionManager sm = ((AppContext) getWebComponent().getBeanContext()).getAppSessionManager();
                WebIServerSession session = sm.getActiveSession();

                // Create a new search object
                WebObjectsFactory factory = session.getFactory();
                WebObjectSource source = factory.getObjectSource();

                WebSearch search = source.getNewSearchObject();

                // Search for MAP_GROUP
                search.setNamePattern(MAP_GROUP);
                search.types().add(new Integer(EnumDSSXMLObjectSubTypes.DssXmlSubTypeUserGroup));
                search.setDomain(EnumDSSXMLSearchDomain.DssXmlSearchDomainRepository);
                search.setSearchFlags(search.getSearchFlags() | EnumDSSXMLSearchFlags.DssXmlSearchNameWildCard);
                search.setAsync(false);
                search.submit();

                // Fetch MapGroup if available
                WebFolder folder = search.getResults();

                if (folder.size() > 0) {
                    mapGroup = (WebUserGroup) folder.get(0);
                }


            }

        } catch (WebObjectsException ex) {// The group couldn't be find. Make sure the MAP_GROUP constant points to an existing group in the metadata.
        }

        return mapGroup;
    }

	
}